package book;

import java.util.ArrayList;
import java.util.List;

/**
 * 书架类,用来存放书籍
 */

public class BookList {
    // books存放了所有当前书架中的书，每个Book对象就是一本书的实体
    private static List<Book> books = new ArrayList<>();
    // booksName存放了所有的书本名称 - 查询书籍等都是通过书名来操作
    private static List<String> booksName = new ArrayList<>();

    // 初始化books和booksName,默认将四大名著放入书架中
    // 使用static代码块初始化static变量
    static{
        books.add(new Book("红楼梦","曹雪芹",59.9,"小说"));
        books.add(new Book("西游记","吴承恩",45.4,"小说"));
        books.add(new Book("水浒传","施耐庵",50.6,"小说"));
        books.add(new Book("三国演义","罗贯中",40.3,"小说"));

        booksName.add("红楼梦");
        booksName.add("西游记");
        booksName.add("水浒传");
        booksName.add("三国演义");
    }

    /**
     * 由书架类提供的方法
     */
    //打印所有书本的信息
    public void displayAllBook(){
        for (Book book:books
             ) {
            System.out.println(book);
        }
    }
    //通过书名查询该书本是否存在
    public boolean contains(String name){
        return booksName.contains(name);
    }
    //通过书本名称找到对应的书本对象,并返回
    public Book findBook(String name){
        int index = booksName.indexOf(name);
        return books.get(index);
    }
    //删除书籍,和书籍名称的list
    public void delBook(String name){
        int index = booksName.indexOf(name);
        books.remove(index);
        booksName.remove(index);
    }
    //添加书籍
    public void add(Book newBook){
        books.add(newBook);
        booksName.add(newBook.getName());
    }
}
